package com.li.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.li.domain.Admin;
import com.li.domain.Menu;
import com.li.service.AdminService;
import com.li.service.MenuService;
import com.li.utlis.MenuUtil;
import com.li.utlis.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.server.PathParam;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    AdminService adminService;

    @Autowired
    MenuService menuService;

    @RequestMapping("/admin/login")
    public JSONObject login(@RequestParam(name = "username",required = false) String username,
                            @RequestParam(name = "password",required = false) String password){
        Admin admin = adminService.login(username, password);
        List<Menu> menuList=null;
        if (admin!=null){
            String menuIds[]=admin.getMenuIds().split(",");
            //menuList = menuService.listByIds(Arrays.asList(menuIds));
            menuList = menuService.listByIdsAndOrderNum(Arrays.asList(menuIds));
        }
        return R.loginAdmin(admin,menuList);
    }
}
